package com.jy.wms.controller;

import com.github.pagehelper.Page;
import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.PropertyUtil;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.pojo.WmsQualityInspectionList;
import com.jy.wms.service.WmsQualityInspectionListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: WmsQualityInspectionListController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author Jane
 * @Date Mon Nov 06 14:46:05 CST 2017
 */
@Controller
@RequestMapping("/wmsqualityinspectionlist")
@Api(value = "/wmsqualityinspectionlist", description = "质检单")
public class  WmsQualityInspectionListController extends BaseController<WmsQualityInspectionListService,WmsQualityInspectionList> {
    @ApiOperation(value = "质检单 POST请求查询质检单信息",response = JSONObject.class, notes = "POST请求查询质检单信息")
    @PostMapping(value="/list")
    public @ResponseBody JSONObject getWmsQualityInspectionList(@RequestBody WmsQualityInspectionList entity, HttpSession session) {
        Page<WmsQualityInspectionList> list = new Page<WmsQualityInspectionList>();
        JSONObject json = getJSONObject();
        try {
            //获取当前操作用户的货主id
            List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            entity.setSshzid(hz.get(0).getHzid());
            //获取仓库Id
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            entity.setCkid(userinfo.getUser().getOrgId().get(0).toString());
            entity.setCkmc(userinfo.getUser().getOrgName());
            list=baseService.baseSelectPage(entity);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.getTotal());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
    @ApiOperation(value = "质检入库任务页面 POST请求查询质检入库任务信息",response = JSONObject.class, notes = "POST请求查询质检入库任务信息")
    @PostMapping(value="/rkRwList")
    public @ResponseBody JSONObject getRkRwList(@RequestBody WmsQualityInspectionList entity, HttpSession session) {
        Page<WmsQualityInspectionList> list = new Page<WmsQualityInspectionList>();
        JSONObject json = getJSONObject();
        try {
            //获取当前操作用户的货主id
            List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            entity.setSshzid(hz.get(0).getHzid());
            //获取仓库Id
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            entity.setCkid(userinfo.getUser().getOrgId().get(0).toString());
            entity.setCkmc(userinfo.getUser().getOrgName());
            list=baseService.baseRkrwSelectPage(entity);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.getTotal());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
    @ApiOperation(value = "质检出库任务页面 POST请求查询质检出库任务信息",response = JSONObject.class, notes = "POST请求查询质检出库任务信息")
    @PostMapping(value="/ckRwList")
    public @ResponseBody JSONObject getCkRwList(@RequestBody WmsQualityInspectionList entity, HttpSession session) {
        Page<WmsQualityInspectionList> list = new Page<WmsQualityInspectionList>();
        JSONObject json = getJSONObject();
        try {
            //获取当前操作用户的货主id
            List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            entity.setSshzid(hz.get(0).getHzid());
            //获取仓库Id
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            entity.setCkid(userinfo.getUser().getOrgId().get(0).toString());
            entity.setCkmc(userinfo.getUser().getOrgName());
            list=baseService.baseCkRwSelectPage(entity);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.getTotal());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
    @ApiOperation(value = "查询全部用户",response = JSONObject.class, notes = "POST查询全部用户")
    @PostMapping(value = "/selectYh")
    public @ResponseBody JSONArray list( HttpSession session,@RequestBody Map<String,Object> map){
        JSONArray json = new JSONArray();
        try {
            String name="";
            if(map.containsKey("name")){
                name=map.get("name").toString();
            }
            map.put("name",name);
            map.put("dCode","hz");
            //获取当前操作用户的货主id
            List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            map.put("value",hz.get(0).getHzid());
            //获取仓库Id
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            map.put("orgId",userinfo.getUser().getOrgId().get(0));
            String host = PropertyUtil.getProperty("safety.host");
            String port = PropertyUtil.getProperty("safety.port");
            String result = HttpUtil.doPost("http://"+host+":"+port+"/ms-safety/extrainterface/gethz", JSONObject.fromObject(map).toString());
            json=JSONArray.fromObject(result);
        } catch (Exception e) {
            e.printStackTrace();
           //return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"查询全部用户失败",e.getMessage(),"");
        }
        return json;
}
    @ApiOperation(value = "质检出入库任务单 POST请求查询PDA登录人员",response = JSONObject.class, notes = "POST请求查询PDA登录人")
    @PostMapping(value="/selectPDADLByHz")
    public @ResponseBody  List<Map<String,Object>> selectPDADLByHz(HttpSession session,@RequestBody Map<String,Object> map) {
        List<Map<String,Object>> list = new ArrayList<>();
        JSONObject json = getJSONObject();
        try {
            String dlyhmc="";
            if(map.containsKey("name")){
                dlyhmc=map.get("name").toString();
            }
            //获取当前操作用户的货主id
            List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);

            //获取仓库Id
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            list=baseService.baseSelectPDADLByHz(hz.get(0).getHzid(),userinfo.getUser().getOrgId().get(0).toString(),dlyhmc);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.size());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
    @PostMapping(value = "/addZjckrw")
    @ApiOperation(value = "质检出库任务单 POST生成质检出库任务",response = JSONObject.class, notes = "POST生成质检出库任务")
    public @ResponseBody JSONObject addZljy(@RequestBody Map<String,Object> map) {
        try {
            baseService.addZjckrw(map.containsKey("zjdid")?map.get("zjdid").toString():"",map.containsKey("ryid")?map.get("ryid").toString():"");
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"生成质检出库任务成功","","");
        }catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(),"生成质检出库任务失败",e.getMessage(),"");
        }catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"生成质检出库任务成功","","");
        } catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"生成质检出库任务失败",e.getMessage(),"");
        }
    }
    @PostMapping(value = "/addZjrkrw")
    @ApiOperation(value = "质检入库任务单 POST生成质检入库任务",response = JSONObject.class, notes = "POST生成质检入库任务")
    public @ResponseBody JSONObject addZjrkrw(@RequestBody Map<String,Object> map) {
        try {
            baseService.addZjrkrw(map.containsKey("zjdid")?map.get("zjdid").toString():"",map.containsKey("ryid")?map.get("ryid").toString():"");
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"生成质检入库任务成功","","");
        }catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(),"生成质检入库任务失败",e.getMessage(),"");
        }catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"生成质检入库任务成功","","");
        } catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"生成质检入库任务失败",e.getMessage(),"");
        }
    }
}
